package com.example.campuslink.viewModel.search

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.campuslink.commen.db.invitation.InvitationTable
import com.example.campuslink.commen.db.school.SchoolInfoTable
import com.example.campuslink.commen.db.userInfo.UserInfoTable
import com.example.campuslink.commen.db.userNumber.UserNumberTable
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.model.SearchWordData
import com.example.campuslink.viewModel.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class SearchResultViewModel : BaseViewModel() {
    private val _schoolAllInvitation = MutableLiveData<List<InvitationTable>>(mutableListOf())
    val schoolAllInvitation get() = _schoolAllInvitation
    var userNumberInfo: UserNumberTable? = null

    fun getInvitation(content: String) {
        val temp = mutableListOf<InvitationTable>()
        viewModelScope.launch(Dispatchers.IO) {
            userNumberInfo = userNumberDao.getUserNumber(
                Sp.getString(
                    context ?: return@launch,
                    SpKey.USER_NUMBER.key ?: "",
                    ""
                ) ?: ""
            )!!
            val schoolInfo =
                schoolInfoDao.getSchoolInfoBySchoolName(userNumberInfo?.schoolName ?: "")

            schoolInfo.mainInvitationList.forEach {
                if (it.invitationTitle.contains(content) ||
                    it.invitationType.contains(content) ||
                    it.invitationContent.contains(content)
                ) {
                    temp.add(it)
                }

            }
            schoolInfo.shoppingInvitationList.forEach {
                if (it.invitationTitle.contains(content) ||
                    it.invitationType.contains(content) ||
                    it.invitationContent.contains(content)
                ) {
                    temp.add(it)
                }
            }
            _schoolAllInvitation.postValue(temp)

        }
    }

    fun addSearchInfo(searchContent: String) {
        viewModelScope?.launch(Dispatchers.IO) {
            val userNumberInfo = userNumberDao?.getUserNumber(
                Sp.getString(
                    context ?: return@launch,
                    SpKey.USER_NUMBER.key ?: "",
                    ""
                ) ?: return@launch
            )
            val schoolInfoTable =
                schoolInfoDao?.getSchoolInfoBySchoolName(userNumberInfo?.schoolName ?: "")

            val searchTable = searchDao?.getSearchData(userNumberInfo?.telNumber ?: "")
            searchTable?.searchData?.removeAll { it.searchWord == searchContent }
            var found = false
            schoolInfoTable?.searchData?.forEach {
                if (it.searchWord == searchContent) {
                    it.searchCount++
                    found = true
                }
            }
            if (!found) {
                schoolInfoTable?.searchData?.add(
                    SearchWordData(
                        searchWord = searchContent,
                        searchCount = 1
                    )
                )
            }

            schoolInfoDao?.updateSchoolInfo(schoolInfoTable ?: SchoolInfoTable())
            searchTable?.searchData?.add(SearchWordData().apply {
                searchWord = searchContent
            })
            searchDao?.updateSearchData(searchTable!!)
        }
    }
}